import moment from 'moment'
Page({
    data: {
        title:0,
        phone:'6',
        detail:'',
        price:'',
        location:'',
        coverPic:'',
        fileList: [],
        openId:'',
        userInfo:{}
    },
    onShow: function (options) {
        const userInfo = wx.getStorageSync('userInfo');
        const openId = wx.getStorageSync('openid');
          this.setData({
            openId,
            userInfo
          })
       
    },


    //获取详细地址
    chooseLocation(){
        wx.chooseLocation({
            success:res=>{
              this.setData({
                  location:res.address
              })
            }
        })
    },

  //图片预览
    previewImg: function(event) {
        var src = event.detail.url; 
        var imgList = []; 
        const {
          fileList = []
        } = this.data
        fileList.forEach((item, index, fileList) => {
          imgList.push(item.url);
        })
        //图片预览
        wx.previewImage({
          current: src, // 当前显示图片的http链接
          urls: imgList // 需要预览的图片http链接列表
        })
      },
    
    
      //删除图片
      deleteToCloud: function(event) {
        
        let fileID = event.detail.file.url
        let index = event.detail.index
        let fileList_new = this.data.fileList
        wx.showLoading({
          title: '正在删除',
        });
        wx.cloud.deleteFile({
          fileList: [fileID],
          success: res => {
            console.log("图片删除成功", res)
            fileList_new.splice(index, 1);
            wx.hideLoading();
            wx.showToast({
              icon: 'success',
              title: '删除成功'
            })
            this.setData({
              fileList: fileList_new
            })
          },
          fail: err => {
            wx.hideLoading();
            wx.showToast({
              icon: 'none',
              title: '删除失败'
            })
            console.log(err)
          }
        })
      },
    
    
    //上传图片到云存储
      uploadToCloud: function(event) {
      
        const {
          fileList = []
        } = this.data
       
    
        const {
          file
        } = event.detail
       
        file.forEach((item, index, file) => {
          
          wx.showLoading({
            title: '正在提交',
          });
          wx.cloud.uploadFile({
            cloudPath: `goods/${(new Date()).getTime() + Math.floor(9 * Math.random())}` + ".jpg", // 上传至云端的路径
            filePath: item.path, // 小程序临时文件路径
            success: res => {
              // 返回文件 ID
              fileList.push({
                url: res.fileID
              });
              this.setData({
                fileList
              });
              wx.hideLoading();
            },
            fail: function() {
              wx.hideLoading();
              console.error
            }
          })
        });
      },
    
  
    
      publich(event){
        console.log(event)
          //从云存储获取封面
            wx.cloud.getTempFileURL({
                fileList: [{
                  fileID:this.data.fileList[0].url,
                }]
              }).then(res => {
                this.setData({
                    coverPic:res.fileList[0].tempFileURL
                })

                // 将求购数据插入到数据库里
                wx.request({
                    url: 'http://127.0.0.1:6060/api/addFreeGoods',
                    header: {"Content-Type": "application/json; charset=utf-8"},
                    method: 'post',
                    data:{
                        openId:this.data.openId,
                        userInfo:this.data.userInfo,
                        coverPic:this.data.coverPic,
                        location:this.data.location,
                        phone: event.detail.value.phone,
                        price: event.detail.value.price,
                        detail: event.detail.value.detail,
                        title: event.detail.value.title,
                        time:moment().format('YY-MM-DD HH:mm:ss')
                    },
                    success: (result) => {
                        console.log(result)
                        }
                })
              }).catch(error => {
                wx.showToast({
                    icon: 'none',
                    title: '发布失败,请稍后再试'
                  })
              })
           
    },

})

